package com.shanghaiyimu.yimiyangche.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.HotFragmentViewPagerAdapter;
import com.shanghaiyimu.yimiyangche.adapter.MyFragmentViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class WhatBanKuaiFragment extends Fragment {


    public WhatBanKuaiFragment() {
        // Required empty public constructor
    }

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> titleList;
    private List<Fragment> fragmentList;
    private Fragment f1,f2,f3,f4;
    private HotFragmentViewPagerAdapter adapter;
    private FragmentManager manager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_what_ban_kuai, container, false);
        initView(view);
        initDaTa();
        return view;
    }

    private void initDaTa() {
        fragmentList = new ArrayList<>();
        fragmentList.add(f1);
        fragmentList.add(f2);
        fragmentList.add(f3);
        fragmentList.add(f4);


        titleList = new ArrayList<>();
        titleList.add("最新回复");
        titleList.add("最新发布");
        titleList.add("问答");
        titleList.add("精华");

        adapter = new HotFragmentViewPagerAdapter(manager,fragmentList,titleList);
        viewPager.setAdapter(adapter);



        //设置tab的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        //添加tab选项卡
        for (int i = 0; i < titleList.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText("Tab"+i));
        }
        //把TabLayout和ViewPager关联起来
        tabLayout.setupWithViewPager(viewPager);


    }

    private void initView(View view) {

        manager = getFragmentManager();
        tabLayout = (TabLayout) view.findViewById(R.id.fragment_ban_kuai_tablayout);
        viewPager = (ViewPager) view.findViewById(R.id.fragment_ban_kuai_tviewpager);
        f1 = new LastestAnswerFragment();
        f2 = new LastestQuestionFragment();
        f3 = new QuestAnswerFragment();
        f4 = new EssenceFragment();


    }

}
